import { observable, action, autorun,toJS } from 'mobx';

import {WeaTools} from "ecCom"
import objectAssign from 'object-assign';
import {message} from 'antd';
import isEqual from 'lodash/isEqual';
import {WeaTableNew} from 'comsMobx'
const {TableStore} = WeaTableNew;
const {ls} = WeaTools;
import * as  Apis from '../apis/add';
import {AddProjectStore} from "./addProjectStore"

class AddStore {
    prjtypes = [];
    @observable showDatas = {
		typesShow : [],
		typesCols : [],
		usedBeans : [],
		abcBtns : [],
		commonuse :'',
		user : ''
	}
	
	@observable	mulitcol = ls.getStr('prj-add-mulitcol') == 'false' ? false : true; //true:four false:one
    @observable isAbc = false
	@observable	abcSelected = "";
	@observable	loading = false;

	@observable addProjectStore = new AddProjectStore();


    @action 
    initDatas(params={}){
		this.loading = true;
		Apis.getAddProjectTypes(params).then((data)=>{
			this.loading = false;
			this.prjtypes = data;
			this.showDatas = {...this.showDatas};
			this.setUpdate();
		});
    }

    //设置选中按钮
 	setAbcSelected(v){
        this.abcSelected = v;
        this.setUpdate();
    }
    //切换列数
	setMulitcol(bool){
		this.mulitcol  = bool;
		this.setUpdate();
	}
	
	//是否abc
	setIsAbc(bool){
		this.isAbc = bool;
		this.setUpdate();
	}
    
    	//刷新数据
	setUpdate(){
		const showDatas = {};
 	
		//setTypesShow
		const {prjtypes,isAbc,tabkey,abcSelected,value = ''} = this;

		
		let typesShow = [];
		let abcBtns = [];
		prjtypes.map(w=>{
			let wNew = objectAssign({},w);
			
			typesShow.push(wNew);
		})
		if(isAbc){
    		let typesABC = [];
    		const colorarray = ["#55D2D4","#B37BFA","#FFC62E","#8DCE36","#37B2FF","#FF9537","#FF5E56"];
			for(let i=0; i<27; i++){
        		let prjbeansAbc = [];
        		typesABC.push({
        			"letter": i == 26 ? "···" : String.fromCharCode(65+i),
        			"prjbeans":function(){
	        			typesShow.map(t=>{
    						t.prjbeans.map(b=>{
								b.letter.charCodeAt(0) - 65 == i && prjbeansAbc.push(b);
								i == 26 && (b.letter.charCodeAt(0) >= 91 || b.letter.charCodeAt(0) < 65) && prjbeansAbc.push(b);
        					});
        				});
        				return prjbeansAbc;
        			}(),
        			"color": colorarray[i%7],
        			"disabled": prjbeansAbc.length <= 0,
        			"selected": abcSelected == (i == 26 ? "···" : String.fromCharCode(65+i))
        		});
			}
			abcBtns = typesABC;
			typesShow = typesABC;
		}
		typesShow = typesShow.filter(s=>{
			const {prjbeans =[]} = s;
			return prjbeans.length > 0;
		});
		showDatas.typesShow = typesShow;
		showDatas.abcBtns = abcBtns;
	
		//setTypesCols
		const docWidth = document.documentElement.clientWidth;
		let typesCols = docWidth > 1400 ? [[],[],[],[]] : (docWidth > 1100 ? [[],[],[]] : (docWidth > 600 ? [[],[]] : [[]]));
		let colHeight = docWidth > 1400 ? [0,0,0,0] : (docWidth > 1100 ? [0,0,0] : (docWidth > 600 ? [0,0] : [0]));
		typesShow.length > 0 && typesShow.map(t=>{
			const {prjbeans = []} = t;
			if(prjbeans.length >= 0){
				let minH = Math.min.apply(Math, colHeight);
	   			for(let i = 0 ;i < colHeight.length;i++){
	   				if(colHeight[i] == minH){
	   					typesCols[i].push(t);
	   					colHeight[i] += prjbeans.length;
	   					break;
	   				}
	   			}
			}
		});
		showDatas.typesCols  = typesCols;
	
		//setUsedBeans
		let usedBeans = [];
		typesShow.length > 0 && typesShow.map(t=>{
			const {prjbeans =[]} = t;
			prjbeans.map(b=>{
				b && b.usedtodo == '1' && usedBeans.length < 10 && usedBeans.push(b)
			});
		})
		usedBeans.sort((a,b)=>{
			return a.usedtodoorder - b.usedtodoorder;
		})
		showDatas.usedBeans = usedBeans;
		this.showDatas = {...this.showDatas,...showDatas};
 	}
	 clearStatus =()=>{
        this.showDatas = {
			typesShow : [],
			typesCols : [],
			usedBeans : [],
			abcBtns : [],
			commonuse :'',
			user : ''
		}
    }



}
const addStore = new AddStore()
export default addStore;